package msb.class02;

import org.junit.Assert;
import org.junit.Test;
import common.util.ArrayUtil;

import java.util.LinkedList;
import java.util.Stack;

/**
 * 如何用栈结构实现队列
 * <p>
 * 实现：
 * 2个栈倒腾下
 */
public class Code07_CodeQueueWithStack {

    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();


    public void add(Integer num) {
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
        stack1.push(num);
    }

    private Integer poll() {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }

    @Test
    public void test() {
        int[] instant = ArrayUtil.instant(100, 100);
        LinkedList<Integer> linkedList = new LinkedList<>();
        Code07_CodeQueueWithStack code07_codeQueueWithStack = new Code07_CodeQueueWithStack();
        for (int i : instant) {
            linkedList.add(i);
            code07_codeQueueWithStack.add(i);
        }

        for (int i : instant) {
            Assert.assertEquals(linkedList.poll(), code07_codeQueueWithStack.poll());
        }


    }

}
